package com.t216.dm216.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.t216.dm216.mapper.DmSchedulerSeatPriceMapper;
import com.t216.dm216.pojo.DmSchedulerSeat;
import com.t216.dm216.pojo.DmSchedulerSeatPrice;
import com.t216.dm216.pojo.dto.DmSchedulerSeatPriceDTO;
import com.t216.dm216.pojo.dto.DmSeatPriceOrderDTO;
import com.t216.dm216.service.DmSchedulerSeatPriceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

@Service
public class DmSchedulerSeatPriceServiceImpl implements DmSchedulerSeatPriceService {
    @Resource
    private DmSchedulerSeatPriceMapper dmSchedulerSeatPriceMapper;
    @Override
    public List<DmSchedulerSeatPriceDTO> getItemPriceByScheduleId(Long scheduleId) {
        MPJLambdaWrapper<DmSchedulerSeatPrice> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.select(DmSchedulerSeatPrice::getId,DmSchedulerSeatPrice::getPrice,DmSchedulerSeatPrice::getScheduleId);
        queryWrapper.selectFunc("IF(COUNT(%s)>0,1,0)",ext->ext.accept(DmSchedulerSeatPrice::getId),DmSchedulerSeatPriceDTO::getIsHaveSeat);
//        queryWrapper.selectAs("IF(COUNT(id)>0,1,COUNT(id))",DmSchedulerSeatPriceDTO::getIsHaveSeat);
        queryWrapper.innerJoin(DmSchedulerSeat.class,DmSchedulerSeat::getScheduleId,DmSchedulerSeat::getScheduleId);
        queryWrapper.eq(DmSchedulerSeat::getAreaLevel,DmSchedulerSeatPrice::getAreaLevel);
        queryWrapper.groupBy(DmSchedulerSeatPrice::getAreaLevel);
        queryWrapper.eq(DmSchedulerSeatPrice::getScheduleId,scheduleId);
        return dmSchedulerSeatPriceMapper.selectJoinList(DmSchedulerSeatPriceDTO.class, queryWrapper);
    }

    @Override
    public List<DmSchedulerSeatPrice> getSeatPriceList(Integer scheduleId) {
        MPJLambdaWrapper<DmSchedulerSeatPrice> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.select(DmSchedulerSeatPrice::getAreaLevelName,DmSchedulerSeatPrice::getPrice, DmSchedulerSeatPrice::getAreaLevel);
        queryWrapper.eq(DmSchedulerSeatPrice::getScheduleId,scheduleId);
        return dmSchedulerSeatPriceMapper.selectList(queryWrapper);
    }

    @Override
    public List<DmSeatPriceOrderDTO> getSeatPriceOrderInfo(Long scheduleId, String[] seatArray) {
        MPJLambdaWrapper<DmSchedulerSeatPrice> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.select(DmSchedulerSeat::getX, DmSchedulerSeat::getY);
        queryWrapper.select(DmSchedulerSeatPrice::getPrice);
        queryWrapper.selectAs(DmSchedulerSeat::getId,DmSeatPriceOrderDTO::getScheduleSeatId);
        queryWrapper.innerJoin(DmSchedulerSeat.class, DmSchedulerSeat::getScheduleId, DmSchedulerSeatPrice::getScheduleId);
        queryWrapper.eq(DmSchedulerSeat::getAreaLevel, DmSchedulerSeatPrice::getAreaLevel);
        //写外面传进来的条件了
        queryWrapper.eq(DmSchedulerSeatPrice::getScheduleId, scheduleId);
        //下面这个麻烦 我们不知道有多少座位
        Consumer<MPJLambdaWrapper<DmSchedulerSeatPrice>> childWrapper = item->{
            for (String seatInfo : seatArray) {
                String [] seats = seatInfo.split("_");
                item.or(wrapper->
                        wrapper.eq(DmSchedulerSeat::getX,seats[0]).eq(DmSchedulerSeat::getY,seats[1])
                );
            }
        };

        queryWrapper.and(childWrapper);

        return dmSchedulerSeatPriceMapper.selectJoinList(DmSeatPriceOrderDTO.class, queryWrapper);
    }
}
